package com.example.soa2.controller;

import com.example.soa2.common.CommonResponse;
import com.example.soa2.entity.Record;
import com.example.soa2.service.RecordService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/records")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @GetMapping("/{id}")
    @ResponseBody
    public CommonResponse<Record> getRecordById(@PathVariable("id")int id){
        return recordService.getRecordById(id);
    }
    @GetMapping({"", "/"})
    @ResponseBody
    public CommonResponse<List<Record>> getRecords(){
        return recordService.getRecords();
    }

    @DeleteMapping("/id/{id}")
    @ResponseBody
    public CommonResponse<String> deleteById(@PathVariable("id")int id){
        return recordService.deleteById(id);
    }

    @DeleteMapping("/{ids}")
    @ResponseBody
    public CommonResponse<String> deleteById(@PathVariable("ids")List<Integer> ids){
        return recordService.deleteByIds(ids);
    }

    @PostMapping("")
    @ResponseBody
    public CommonResponse<String> postRecords(Record record){
        return recordService.postRecords(record);
    }
    @GetMapping("/{personid}/recordsbyperson")
    @ResponseBody
    public CommonResponse<List<Record>> getRecordsByPersonId(@PathVariable("personid")int personid){
        return recordService.getRecordsByPersonId(personid);
    }
    @GetMapping("/{thingid}/recordsbything")
    @ResponseBody
    public CommonResponse<List<Record>> getRecordsByThingId(@PathVariable("thingid")int thingid){
        return recordService.getRecordsByThingId(thingid);
    }

    @PutMapping("/{id}")
    @ResponseBody
    public CommonResponse<String> updateRecord(@PathVariable("id") int id, @RequestBody Record updatedRecord) {
        System.out.println(updatedRecord);
        return recordService.updateRecord(id, updatedRecord);
    }

}
